package com.snake.springboot.authority.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.snake.springboot.authority.api.model.AuthStrategyModel;
import com.snake.springboot.authority.api.request.AuthStrategyQuery;
import com.snake.springboot.authority.api.service.AuthStrategyService;
import com.snake.springboot.authority.dao.mybatis.entity.AuthStrategyDO;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthStrategyMapper;
import com.snake.springboot.authority.service.assembler.AuthStrategyAssembler;
import org.snake.common.result.ReturnList;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类 {@code AuthStrategyServiceImpl}  <br> .
 * <p>权限策略实现类
 * <p>创建时间：2025/02/07
 *
 * @author Huang.yj
 */
@Service
public class AuthStrategyServiceImpl implements AuthStrategyService {

    private AuthStrategyMapper authStrategyMapper;

    @Autowired
    public AuthStrategyServiceImpl(AuthStrategyMapper authStrategyMapper){
        this.authStrategyMapper = authStrategyMapper;
    }

    @Override
    public AuthStrategyModel addAuthStrategy(AuthStrategyModel model) throws Exception {
        AuthStrategyDO authStrategyDO = AuthStrategyAssembler.assembleAuthStrategyDO(model,true);
        authStrategyMapper.insert(authStrategyDO);
        model.setStrategyId(authStrategyDO.getStrategyId());
        return model;
    }

    @Override
    public AuthStrategyModel modifyAuthStrategy(AuthStrategyModel model) throws Exception {
        AuthStrategyDO authStrategyDO = AuthStrategyAssembler.assembleAuthStrategyDO(model,false);
        authStrategyMapper.update(authStrategyDO);
        return model;
    }

    @Override
    public int removeAuthStrategy(String strategyId) {
        return authStrategyMapper.delete(strategyId);
    }

    @Override
    public AuthStrategyModel queryAuthStrategy(String strategyId) {
        AuthStrategyDO authStrategyDO = authStrategyMapper.findById(strategyId);
        AuthStrategyModel result = AuthStrategyAssembler.assembleAuthStrategyModel(authStrategyDO);
        return result;
    }

    @Override
    public List<AuthStrategyModel> queryAuthStrategyList(AuthStrategyModel model) throws Exception {
        AuthStrategyDO condition = AuthStrategyAssembler.assembleAuthStrategyDO(model,false);
        List<AuthStrategyDO> authStrategyDOs = authStrategyMapper.findByCondition(condition);
        List<AuthStrategyModel> result = ListUtils.copy(authStrategyDOs,AuthStrategyModel.class);
        return result;
    }

    @Override
    public ReturnList<AuthStrategyModel> queryAuthStrategyList(AuthStrategyQuery query) throws Exception {
        AuthStrategyDO condition = AuthStrategyAssembler.assembleAuthStrategyDO(query.getParams(),false);
        Page<?> page = PageHelper.startPage(query.getPage().getPageNum(),query.getPage().getPageSize());
        List<AuthStrategyDO> authStrategyDOs = authStrategyMapper.findByCondition(condition);
        List<AuthStrategyModel> authStrategyModels = ListUtils.copy(authStrategyDOs,AuthStrategyModel.class);
        ReturnList<AuthStrategyModel> result = new ReturnList<AuthStrategyModel>(authStrategyModels,page.getTotal());
        return result;
    }
}
